import { _cos } from '../math/index.js'
import { isUndefined } from '../utils/index.js'

export const rgbaWrapper = (rgbaArr) => { //[1,1,1,1]
    return (rgbaArr.length === 4 ? 'rgba(' : 'rgb(') + rgbaArr + ')'
}

//色相环
export const colorCircle = (n = 36, alpha = 1) => {
    var a = 0,
        step = 360 / n,
        arr = [],
        r, g, b;
    for (var i = 0; i < n; i++) {
        a += step
        r = _cos(a) * 127 + 128 << 0;
        g = _cos(a + 120) * 127 + 128 << 0;
        b = _cos(a + 240) * 127 + 128 << 0;
        arr[arr.length] = rgbaWrapper(isUndefined(alpha) ? [r, g, b] : [r, g, b, alpha]);
    }
    return arr;
}


export const genColor = (color, alpha) => {
    //分类颜色
    //红 100
    //绿 010
    //青 001 
    //黄 110 
    //紫 101
    //全彩 111
    //黑白灰 000
    var colorMap = {
        red: [1, 0, 0],
        green: [0, 1, 0],
        syan: [0, 0, 1],
        yellow: [1, 1, 0],
        purple: [1, 0, 1],
        gray: [0, 0, 0],
        colorful: [1, 1, 1]
    }

    var arr = color in colorMap ? colorMap[color] : colorMap["colorful"];
    var r = arr[0],
        g = arr[1],
        b = arr[2];
    if (r & g & b === 1) { //全彩
        arr = arr.map(function (t) {
            return Math.random() * 255 << 0;
        });
    } else if (r & g & b === 0) { //灰
        var t = Math.random() * 255 << 0;
        arr = [t, t, t];
    } else {
        var rgb = 155;
        var c = (Math.random() * (255 - rgb) << 0) + rgb;
        arr = arr.map(function (t) {
            return t === 1 ? (Math.random() * (255 - rgb) << 0) + rgb : Math.random() * (c / 2) << 0;
        });
    }
    if (alpha) arr[arr.length] = alpha;
    // this.color =this.rgbaWrapper(arr) 
    return rgbaWrapper(arr)

}

const random = Math.random;

/**
 * 获取随机颜色
 * @returns { Object } 颜色对象
 */
export const randomColor = () => {
    return {
        r: random() * 255,
        g: random() * 255,
        b: random() * 255,
        a: random() * 1
    }
}
// 生成颜色
export const genColors = (color, n = 10, alpha = 1) => {
    let colors = []
    for (let i = 0; i < n; i++) {
        colors[colors.length] = genColor(color, alpha)
    }
    return colors
}

// const redColors = (n, alpha) => {
//     return genColors('red', n, alpha)
// }

// const greenColors = (n, alpha) => {
//     return genColors('green', n, alpha)
// }





export const colorsFnMap = {
    'none': '',
    colorCircle
};
// 'yellow',
['red', 'green', 'syan', 'purple', 'gray', 'random'].forEach(t => {
    colorsFnMap[`${t}Colors`] = (n, alpha) => {
        return genColors(t, n, alpha)
    }
});

export const colorTypes = Object.keys(colorsFnMap)

export const _colors = (color, n, alpha = 1) => {
    return colorsFnMap[color] ? colorsFnMap[color](n, alpha) : colorCircle(n, alpha)
}


// let colors = colorsFnMap[color] ? colorsFnMap[color](n, 0.5) : colorCircle(n, 0.5)

//生成11*11的二维数组（颜色）要求：色卡左上角为白色，左下角为黑色，右上角为输入的颜色，右下角亮度为0输入颜色
function createColorList(h, n, m) {
    var list = [];
    for (var i = 0; i <= 10; i++) {
        for (var y = 0; y <= 10; y++) {
            if (y === 0) {
                list[i] = [];
            }
            var s = y * (n / 10);
            var l = 100 - (i * 10);
            var _m = m - (m / 10 * i);
            l = l - (y * ((l - _m) / 10));
            var color = hsvToRgb([h, s / 100, l / 100]);
            var item = 'rgb(' + color[0] + ',' + color[1] + ',' + color[2] + ')'
            list[i].push(item)
        }
    }
    return list;
}



//hsv颜色转rgb颜色
function hsvToRgb(arr) {
    var h = arr[0], s = arr[1], v = arr[2];
    var r = 0, g = 0, b = 0;
    var i = parseInt((h / 60) % 6);
    var f = h / 60 - i;
    var p = v * (1 - s);
    var q = v * (1 - f * s);
    var t = v * (1 - (1 - f) * s);
    switch (i) {
        case 0: r = v; g = t; b = p; break;
        case 1: r = q; g = v; b = p; break;
        case 2: r = p; g = v; b = t; break;
        case 3: r = p; g = q; b = v; break;
        case 4: r = t; g = p; b = v; break;
        case 5: r = v; g = p; b = q; break;
        default: break;
    }
    r = parseInt(r * 255.0)
    g = parseInt(g * 255.0)
    b = parseInt(b * 255.0)
    return [r, g, b];
}
//rgb颜色转hsv颜色
function rgbToHsv(arr) {
    var h = 0, s = 0, v = 0;
    var r = arr[0], g = arr[1], b = arr[2];
    arr.sort(function (a, b) {
        return a - b;
    })
    var max = arr[2]
    var min = arr[0];
    v = max / 255;
    s = max === 0 ? 0 : 1 - (min / max);
    if (max === min) {
        h = 0; //事实上，max===min的时候，h无论为多少都无所谓
    } else if (max === r && g >= b) {
        h = 60 * ((g - b) / (max - min)) + 0;
    } else if (max === r && g < b) {
        h = 60 * ((g - b) / (max - min)) + 360
    } else if (max === g) {
        h = 60 * ((b - r) / (max - min)) + 120
    } else if (max === b) {
        h = 60 * ((r - g) / (max - min)) + 240
    }
    h = parseInt(h);
    s = parseInt(s * 100);
    v = parseInt(v * 100);
    return [h, s, v]
}

